package u1project.server;


import u1project.common.Request;
import u1project.common.Response;

import java.io.*;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author oldliu
 * @since 1.0
 */
public class MainServer {
    public static void main(String[] args) {
        ExecutorService es= Executors.newFixedThreadPool(10);
        try {
            ServerSocket st=new ServerSocket(9996);
            System.out.println("Server start...");
            while (true){
                Socket s = st.accept();
                es.submit(new ObjectThread(s));
                //es.submit(new SendThread(s));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
class ObjectThread implements Runnable{
    private final Socket s;
    public ObjectThread(Socket s){
        this.s=s;
    }
    @Override
    public void run() {
        System.out.println(s+"连上了");
        try {
            ObjectInputStream oin=new ObjectInputStream(s.getInputStream());
            ObjectOutputStream oout=new ObjectOutputStream(s.getOutputStream());
            Request req=null;
            Response resp=null;
            while (true){
                //
                req= (Request) oin.readObject();
                Method m=this.getClass().getDeclaredMethod(req.getAction(),Request.class);
                resp=(Response) m.invoke(this,req);

//                if(req.getAction().equals("login")){
//                    resp=login(req);
//                }else if(req.getAction().equals("register")){
//                    resp=register(req);
//                }else if(req.getAction().equals("upload")){
//                    resp=upload(req);
//                    //....
//                }else{
//                    //没实现的操作
//                    resp=new Response(false,"您的这个请求"+req.getAction()+"不支持");
//                }
                oout.writeObject(resp);
                oout.reset();//防止对象缓存
                oout.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                s.close();
            } catch (Exception e) {

            }
        }
    }
    public Response login(Request req){
        String name=(String)req.get("name");
        String pass=(String)req.get("password");
        if("laoliu".equals(name)&&"123".equals(pass)){
            return new Response(true);
        }
        return new Response(false,"错误的用户名或者密码");
    }
    //上传，需要一个文件名，还需要一个byte数组
    public Response upload(Request req){
        String name=(String)req.get("novelname");
        byte[] bytes=(byte[])req.get("content");
        File f=new File("D:\\Documents\\桌面\\test",name);
        try {
            FileOutputStream fout=new FileOutputStream(f);
            fout.write(bytes);
            fout.close();
            return new Response(true,"文件保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Response(false,"文件保存失败"+e);
        }

    }
    public Response register(Request req){
        String name=(String)req.get("name");
        String pass=(String)req.get("password");
        String pass2=(String)req.get("repassword");
        if(pass.equals(pass2)==false){
            return new Response(false,"两次密码不一致");
        }

        if("laoliu".equals(name)){
            return new Response(false,"用户名已经存在！");
        }
        return new Response(true,"注册成功");
    }
}